#include "hr.h"
#include "ui_hr.h"
#include <QMessageBox>
#include "myprotocol.h"
#include <cstring>

HR::HR(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HR)
{
    ui->setupUi(this);
    this->setFixedSize(this->size().width(),this->size().height());
    this->ui->pic->setPixmap(QPixmap(":/new/prefix1/Image/write2file.png"));
    this->setWindowIcon(QIcon(":/new/prefix1/Image/mesage.png"));
    this->setWindowTitle("CHOOSE");
    this->ui->pic->setPixmap(QPixmap(":/new/prefix1/Image/sunny.png"));
    QPalette qp = this->palette(); //重新拷贝一份和本对象palette相同的palette
    qp.setBrush(QPalette::Window,QBrush(QImage(":/new/prefix1/Image/Frame.jpg").scaled(this->size())));
    this->setPalette(qp);

    this->socket = nullptr;

    connect(this,&HR::my_clear_text_signal,this,&HR::my_clear_text_slot);

    this->ui->query_btn->setShortcut(Qt::Key_Return);
}

HR::~HR()
{
    delete ui;
}

void HR::set_socket(QTcpSocket* socket)
{
    this->socket = socket;
}

void HR::on_add_btn_clicked()
{
    emit my_add_signal();
    this->ui->staff_line->clear();
}

void HR::on_query_btn_clicked()
{
    if(this->ui->staff_line->text().isEmpty())
    {
        QString error_str = "staff name empty!";
        QMessageBox::critical(this, "error", error_str);
        return;
    }
    char send_info[512] = {0};
    sprintf(send_info,"%d",query_request_protocol);
    trans_dbinfo_t myinfo;
    QByteArray name = this->ui->staff_line->text().toUtf8();
    strcpy(myinfo.name,name.data());
    memcpy(send_info + 2,&myinfo,sizeof(myinfo));

    this->socket->write(send_info,sizeof(send_info));
}

void HR::on_selectstaff_btn_clicked()
{
    QString mydata = QString::number(select_all_request_protocol);
    mydata += "no message";
    this->socket->write(mydata.toUtf8());

    this->ui->staff_line->clear();
}

void HR::my_clear_text_slot()
{
    this->ui->staff_line->clear();
}

void HR::on_back_btn_clicked()
{
    emit this->my_back_to_widget_signal();
    this->hide();
}
